package cn.com.scan.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import cn.com.scan.config.Config;
import cn.com.scan.config.Constants;
import cn.com.scan.entity.Storage;
import cn.com.scan.entity.User;
import cn.com.scan.tools.SharedPreferencesUtils;

public abstract class BaseActivity extends FragmentActivity {
	Storage storage;
	static User user;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//注册退出的广播
		IntentFilter filter = new IntentFilter();
		filter.addAction(Config.Exit_Program_TAG);
		String  ip= SharedPreferencesUtils.getIP(this);
		registerReceiver(receiver, filter);
		//注册二维码扫描监听的广播
		IntentFilter filter2 = new IntentFilter();
		filter2.addAction(Constants.BAR_READ_ACTION);
		registerReceiver(byReceiver, filter2);
		Config.setIP(ip);
	}
	protected abstract void broadcastExit();
	protected abstract void capturePic(String codeStr);
	// 广播接收器
	BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String tagStr = intent.getAction();
			if (tagStr.equals(Config.Exit_Program_TAG)) {
				broadcastExit();
				System.exit(0);
				finish();
			}
		}
	};
	//广播接受类
	BroadcastReceiver  byReceiver=new BroadcastReceiver(){
		public String action;

		@Override
		public void onReceive(Context context, Intent intent) {
			action = intent.getAction();
			if (Constants.BAR_READ_ACTION.equals(action)) {
				String BAR_value = intent.getStringExtra("BAR_VALUE");
				capturePic(BAR_value);
				Config.LogI("---------BAR_VALUE--------->"+BAR_value);
			}
		}
	};
	//全局保存登陆的用户
	public void   setLoginUser(User user){
		this.user=user;
	}
	//全局保存
	public void  setStorage(Storage storage){
		this.storage=storage;
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(receiver);
		unregisterReceiver(byReceiver);
		super.onDestroy();
	}
}
